package com.gy.zpchat.push;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;


import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.gy.zpchat.R;
import im.zego.zpns.ZPNsMessageReceiver;
import im.zego.zpns.entity.ZPNsMessage;
import im.zego.zpns.entity.ZPNsRegisterMessage;
import com.gy.zpchat.util.AppLogger;

public class MyZIMPushReceiver extends ZPNsMessageReceiver {

    public static Event event;

    @Override
    protected void onThroughMessageReceived(Context context, ZPNsMessage message) {
        Log.e("onThroughMessage", "onNotificationClicked message:" + message.toString());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (nm != null){
                NotificationChannel notificationChannel = new NotificationChannel("aaaa", "ZIM离线推送", NotificationManager.IMPORTANCE_HIGH);
                nm.createNotificationChannel(notificationChannel);
            }
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"aaaa")
                .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("收到消息").setContentText(message.getExtras());
        NotificationManagerCompat.from(context).notify(0x111,builder.build());
    }

    @Override
    protected void onNotificationClicked(Context context, ZPNsMessage message) {
        Log.e("MyZIMPushReceiver", "onNotificationClicked message:" + message.toString());
        if (message.getExtras() != null) {
            event.onNotificationClicked(message.getExtras());
        }
    }

    @Override
    protected void onNotificationArrived(Context context, ZPNsMessage message) {
        Log.e("MyZIMPushReceiver", "onNotificationArrived message:" + message.toString());

    }

    @Override
    protected void onRegistered(Context context, ZPNsRegisterMessage message) {
        if (event != null) {
            event.onRegistered(message.getCommandResult());
        }

        AppLogger.getInstance().d("[MyZIMPushReceiver] [onRegistered] [commandResult:%s] [pushSource:%s] [msg:%s] ]", message.getCommandResult(), message.getPushSource().name(), message.getMsg());
    }

    public interface Event {
        void onRegistered(String regID);

        void onNotificationClicked(String message);
    }
}
